﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyThuVien1.BUS;
using QuanLyThuVien1.DAO;
using QuanLyThuVien1.DTO;

namespace QuanLyThuVien1.GUI
{
    public partial class Fr_PhieuMuon : Form
    {
        public Fr_PhieuMuon()
        {
            InitializeComponent();
        }
        int dem = 0;
        public string ChuanHoa(string m_sChuoi)
        {
            if (m_sChuoi == "")
                return m_sChuoi;
            for (int i = 0; i < m_sChuoi.Length - 1; )
            {
                if (m_sChuoi[i] == 32 && m_sChuoi[i + 1] == 32)
                {
                    m_sChuoi = m_sChuoi.Substring(0, i + 1) + m_sChuoi.Substring(i + 2, m_sChuoi.Length - i - 2);
                }
                else
                    i++;
            }
            if (m_sChuoi[0] == 32 && m_sChuoi.Length == 1)
                return "";

            if (m_sChuoi[0] == 32)
                m_sChuoi = m_sChuoi.Substring(1, m_sChuoi.Length - 1);
            if (m_sChuoi[m_sChuoi.Length - 1] == 32)
                m_sChuoi = m_sChuoi.Substring(0, m_sChuoi.Length - 1);
            return m_sChuoi;
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void dtg_MuonSach_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int soSach = 0;
            Sach_Bus s = new Sach_Bus();
            if (e.ColumnIndex == 5)
            {

                if (this.dtg_MuonSach.Rows[e.RowIndex].Cells[5].Value.Equals(0))
                {
                    dem++;
                    this.dtg_MuonSach.Rows[e.RowIndex].Cells[5].Value = 1; ;
                }
                else
                    if (this.dtg_MuonSach.Rows[e.RowIndex].Cells[5].Value.Equals(1))
                       {
                        this.dtg_MuonSach.Rows[e.RowIndex].Cells[5].Value = 0;
                        dem--;
                        }
                    soSach = s.soSachTheoMa(dtg_MuonSach.Rows[e.RowIndex].Cells[1].Value.ToString());
                    if (dem > 5 && soSach > 0)
                    {
                        MessageBox.Show("Mổi lần mượn tối đa 5 cuốn sách ! Hoặc là sách đó đã hết rồi","Thông Báo");
                        this.dtg_MuonSach.Rows[e.RowIndex].Cells[5].Value = 0;
                    }
                
            }
        }

        private void Fr_PhieuMuon_Load(object sender, EventArgs e)
        {
            try
            {
                CTongQuat t = new CTongQuat();
                dtg_MuonSach.DataSource = t.LayThongTinChoMuonSach_Bus();
                for (int i = 0; i < dtg_MuonSach.Rows.Count - 1; i++)
                {
                    dtg_MuonSach.Rows[i].Cells[0].Value = i + 1;
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void btn_TaoPhieu_Click(object sender, EventArgs e)
        {
            int n = dtg_MuonSach.Rows.Count - 1;
            CChiTietMuon_Bus c = new CChiTietMuon_Bus();
            Sach_Bus s = new Sach_Bus();
            for (int i = 0; i < n; i++)
            {
              
                if (dtg_MuonSach.Rows[i].Cells[5].Value.Equals(1))
                {
                    string maSach = dtg_MuonSach.Rows[i].Cells[1].Value.ToString();
                    string maPM = Fr_FistPhieuMuoncs.maPM;
                    c.ThemChiTietMuon_Bus(maPM, maSach);
                    s.GiamSoSach(maSach);
                    MessageBox.Show("Đã Hoàn Thành","Thông Báo");
                }
            }
        }

        private void bt_Huy_Click(object sender, EventArgs e)
        {
            CPhieuMuon_Bus p = new CPhieuMuon_Bus();
            p.XoaThongTinPhieuMuon(Fr_FistPhieuMuoncs.maPM);
            MessageBox.Show("Đã Hủy Thành Công.Bấm OK để thoát","Thông Báo");
            this.Close();

        }


    }


}